
package com.yimixian.app.cart;

import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.fragment.CartFragment;
import com.yimixian.app.fragment.UnloginFragment;
import com.yimixian.app.goods.GoodDetailTopFragment;
import com.yimixian.app.user.UserRegistrationActivity;
import com.yimixian.app.util.UiUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import de.greenrobot.event.EventBus;

public class CartDetailActivity extends BaseNaviFragmentGroupActivity {
    public TextView mEditCartView;
    public FrameLayout mDialogView;
    public boolean mIsEditState = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.activity_navibar_simple,null);
        mDialogView = (FrameLayout) view.findViewById(R.id.fl_dialog_container);
        setContentView(view);
        setTopNavi(R.drawable.ic_close,
            "", getString(R.string.cart),
            null, null);
        mEditCartView = (TextView) view.findViewById(R.id.title_bar_right_text);
        mEditCartView.setText("编辑");
        mIsEditState = false;
        mEditCartView.setVisibility(View.VISIBLE);
        mEditCartView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(UiUtils.isFastDoubleClick()){
                    return;
                }
                if ("编辑".equals(mEditCartView.getText())) {
                    mIsEditState = true;
                    mEditCartView.setText("完成");
                } else {
                    mIsEditState = false;
                    mEditCartView.setText("编辑");
                }
                if (mCurrentPrimaryFragment != null && mCurrentPrimaryFragment instanceof CartFragment) {
                    ((CartFragment) (mCurrentPrimaryFragment)).updateGoodsView();
                }
            }
        });
    }

    public static Intent buildIntent(Context context) {
        Intent intent = new Intent(context, CartDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        return intent;
    }

    @Override
    protected void initPrimaryFragment() {
        switchPrimaryFragment(0);
    }

    @Override
    protected Class<? extends Fragment> getPrimaryFragmentClass(int fragmentId) {
        return CartFragment.class;
    }

    @Override
    protected Bundle getPrimaryFragmentArguments(int fragmentId) {
        Bundle bundle = new Bundle();
        return bundle;
    }

    @Override
    protected int getPrimaryFragmentStubId(int fragmentId) {
        return R.id.content_frame;
    }

    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.slide_out_to_bottom);
    }

    @Override
    public void onStop() {
        mIsEditState = false;
        if(mEditCartView != null) {
            mEditCartView.setText("编辑");
        }
        EventBus.getDefault().post(new String(Constants.REFRESH_UI));
        super.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == GoodDetailTopFragment.RETURN_TO_CATEGORY){
            YMXMainActivity.needChooseKindTab = true;
            setResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
            finish();
            overridePendingTransition(0, 0);
        }
    }
}
